Intro to Programming (for the beginner)



Okay, if you're reading this, I'm going to assume that your reaction to programming is something like, "Hey, cool. I can make the computer do anything I want it to do." Well, you can, but it's hard work. But, when you play a game that you've written, or use a program to do something, it's all worth it. So, without further ado, on to the tutorial.

Okay, first of all, you're going to need a compiler. You can manage this several ways. If you have money to burn, you can buy a commercial compiler, such as Microsoft Visual C/C++ or commercial Borland C++ Builder. However, if you're like most of us, you don't have a whole lot of money to spend on a compiler. For you, go to http://www.delorie.com/djgpp and download the DJGPP GNU C++ compiler. I know that sound cryptic, but believe me it works. Once you know what it's all about, go to the zip picker, answer the questions there and download the files you need. And now, as well, you have the option of downloading the now-freeware Borland C++ Builder 7 compiler. Unfortunately, it's a text only compiler, but they have good documentation so that you can figure out how to use it. If you have any questions on this part, you can email the webmaster of that page for help. They will usually try to take the time to help you out.

I'll assume that by now you have a compiler on your computer and have at least looked at it. Play around with the menus to find out how to compile a program, etc. so that you know how to do that later. That will be increasingly important as we move through, but for now, just know the basics.
Right now, we will write our first program, so pay attention and I'll explain each part later.

#include <stdio.h>
int main()
{
	/* Print to screen */
	printf("Hey World, how's it going?\n");
	return (1);
}


First, the line:
#include <stdio.h>
This tells the compiler to add the header (I'll explain this later) file named stdio.h (standard i/o) to your program. This header file contains the function "printf". For now, always use the chevrons (< >) to enclose an included file. Later on, when you create your own headers, this will change. The header file is a standard library of functions/commands that is part of the C (or C++) language. They exist so that you can pick and choose what you want to include in your program.

Next, the line:
int main()
This is the body section of your program. When your program executes (runs), the "main" function is called by the computer. This line contains lots of information. The anatomy of it is like this:
<return type> <function name> (<argument 1 type> <argument 1 name>, ... )

Okay, this may not make any sense, but it will later on. For now, just use "int main()" for all your programs and you'll be fine.
The opening curly brace "{" is used to show the start of a function or loop, in this case the main function. Later on the closing curly brace "}" will show the end of the function.
Next, the line:
/* Print to screen */

This is an example of a comment in C. It is used to tell the programmer (or anyone reading the code) whatever needs to be said. Comments are often used to explain cryptic sections of code.
Next, the line:
printf("Hey World, how's it going?\n");

This is how you send information to the screen to be viewed (in C). The function "printf" is called. The information you want to print to the screen is put in quotes "" inside the parentheses which show that printf is a function. This may sound weird now, but just hold on for a second. The \n character means a newline is printed out. This is important to type being displayed correctly on the screen. Last in this line is the semi-colon. NEVER forget to put a semi-colon at the end of a command or function like this! It will cause you much pain in debugging and several headaches, which can be avoided by simply remembering to put in semi-colons where they belong.
Now here is something important: when you run this program you'll notice that on the screen is the message Hey World, how's it going?. Now, whatever you put inside the quotes is what will be printed, so if you wanted to change this to "I like eggs", you can do that.

The line:
return (1);

Might be clear to you. This sends the number 1 (or whatever is in the parentheses) back to the computer as an "exit code" for your function. Often the number 1 is used to indicate successful completion of the task it was designed for and any other number means error. This (when used in the main function) will end your program. Again, NEVER forget to put the semi-colon at the end of the line.
Lastly, the closing curly brace shows the end of the main function.

That is the basics of C programming illustrated using a very, VERY simple program. Later on we will go over data types, program flow (looping), decision making, input, mathematics, data storage and output. But for now, make sure that the above is suitably clear so that you can modify it.
Below are some tasks for you to complete if you feel like you want to test your knowledge of what we've covered. If you have any other suggestions for these problems, feel free to email me at webmaster@vidgamez.iwarp.com. Thanks!

Further tasks:
1) Change the output on the screen from Hey World, how's it going? to I am a programmer now!
2) Add another text output that says whatever you want it to